export default {
    //时间格式化为 几秒前，几天前，几分钟前
    getDateDiff (dateStr) {
        console.log(dateStr);
        let publishTime = new Date(dateStr).getTime()/1000,
            d_seconds,
            d_minutes,
            d_hours,
            d_days,
            timeNow = parseInt(new Date().getTime()/1000),
            d,
            date = new Date(publishTime*1000),
            Y = date.getFullYear(),
            M = date.getMonth() + 1,
            D = date.getDate(),
            H = date.getHours(),
            m = date.getMinutes(),
            s = date.getSeconds();

        //小于10的在前面补0
        if (M < 10) M = '0' + M;
        if (D < 10) D = '0' + D;
        if (H < 10) H = '0' + H;
        if (m < 10) m = '0' + m;
        if (s < 10) s = '0' + s;

        d = timeNow - publishTime;
        d_days = parseInt(d/86400);
        d_hours = parseInt(d/3600);
        d_minutes = parseInt(d/60);
        d_seconds = parseInt(d);

        if(d_days > 0 && d_days < 3){
            return d_days + '天前';
        }else if(d_days <= 0 && d_hours > 0){
            return d_hours + '小时前';
        }else if(d_hours <= 0 && d_minutes > 0){
            return d_minutes + '分钟前';
        }else if (d_seconds < 60) {
            if (d_seconds <= 0) {
                return '刚刚';
            }else {
                return d_seconds + '秒前';
            }
        }else if (d_days >= 3 && d_days < 30){              //大于3天小于30天 则显示 月-日
            return M + '-' + D + ' ' + H + ':' + m;
        }else if (d_days >= 30) {                           //大于30天显示 标准时间格式
            return Y + '-' + M + '-' + D + ' ' + H + ':' + m;
        }
    },
    //默认 简单的日期选择范围
    simpleDate(){
        return [
            {
                text: '今天',
                onClick(picker) {
                    picker.$emit('pick', new Date());
                }
            },
            {
                text: '昨天',
                onClick(picker) {
                    const date = new Date();
                    date.setTime(date.getTime() - 3600 * 1000 * 24);
                    picker.$emit('pick', date);
                }
            },
            {
                text: '一周前',
                onClick(picker) {
                    const date = new Date();
                    date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                    picker.$emit('pick', date);
                }
            }
        ]
    },
    //时间戳转换为标准日期
    toYmdDate(date) {
        let json_date = new Date(date).toJSON();
        return new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
    },
    //格式化 标准格式日期 年月日 时分秒
    formatDate(date) {
        date = new Date(date * 1000);

        let YY = date.getFullYear() + '-';
        let MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
        let hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        let mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        let ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());

        return YY + MM + DD +" "+hh + mm + ss;
    },
    //获取日期格式 年月日
    YmdDate(date) {
        date = new Date(date * 1000);

        let YY = date.getFullYear() + '-';
        let MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());

        return YY + MM + DD;
    },
    //获取年月
    YmDate(date) {
        date = new Date(date * 1000);

        let YY = date.getFullYear() + '-';
        let MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';

        return YY + MM;
    },
    getToday(){
        let now = new Date();
        let year = now.getFullYear();
        let month = (now.getMonth() + 1 < 10) ? '0'+(now.getMonth() + 1) : now.getMonth() + 1  ;
        let date = (now.getDate() + 1 < 10) ? '0'+ now.getDate() : now.getDate() ;

        return year + '-' + month + '-' + date;
    },
    AddDays(dataStr,dayCount){
        let strdate = dataStr;                  // 2017年03月01日，该日期增加dayCount天
        strdate=strdate.replace("年","/");
        strdate=strdate.replace("月","/");
        strdate=strdate.replace("日","/");
        let isdate = new Date(strdate);
        isdate = new Date((isdate/1000+(86400*dayCount))*1000); // dayCount=1
        let year = isdate.getFullYear(); // yyyy
        let month = isdate.getMonth()+1; // M
        let day = isdate.getDate(); // d

        if (month >= 1 && month <= 9) { // MM
            month = "0" + month;
        }

        if (day >= 0 && day <= 9) { // dd
            day = "0" + day;
        }

        return year + "-" + month + "-" + day ;     // pdate=2017年03月02日
    }
}